package net.es.weathermap.topo.ps;

import java.io.IOException;
import java.util.*;
import edu.internet2.perfsonar.PSException;
import edu.internet2.perfsonar.TSLookupClient;
import org.apache.commons.httpclient.HttpException;
import org.jdom.Element;
import net.es.weathermap.topo.network.NetworkInfo;

public class PSTopoPuller {

    public Element pullTopology() throws HttpException, IOException, PSException {
        //String[] tsList = new String[1];
        NetworkInfo networkDetails = NetworkInfo.getInstance();
        ArrayList<String> topoServerList = networkDetails.getTopoServerUrls();
        String domainId = networkDetails.getTopoDomainId();
        String domainString = "urn:ogf:network:domain="+domainId;
        String eventUrl = networkDetails.getTopoEventUrl();
        
        String[] tsList = new String[topoServerList.size()];
        for(int i=0; i< topoServerList.size(); i++){
        	tsList[i] = topoServerList.get(i);
        }
        //tsList[0] = "http://ps3.es.net:8012/perfSONAR_PS/services/topology";
        TSLookupClient psClient = new TSLookupClient();
        psClient.setTSList(tsList);
        Element domain = psClient.getDomain(domainString, eventUrl);

        if (domain == null) {
            throw new IOException("No domain data from topology server");
        }
        return domain;
    }
}
